package com.legsem.jrhosync.samples;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.TreeMap;

import com.legsem.jhosync.Context;
import com.legsem.jhosync.face.SourceAdapter;
import com.legsem.jhosync.face.SourceAdapterException;

/**
 * This is generic code shared by Query load test implementations.
 * <p/>
 * Basically each implementation provides a fixed number if items to return.
 * <p/>
 * We use TreeMaps so that the map order is predictable (and testable).
 * <p/>
 * Query data contains time of day at the second level so it is very likely to
 * be different on each call.
 * 
 */
public class AbstractLoadSource extends SourceAdapter {

    /** */
    private int itemNum;

    public AbstractLoadSource(int itemNum) {
        this.itemNum = itemNum;
    }

    public void login(Context context) throws SourceAdapterException {
        // TODO Auto-generated method stub

    }

    public Map < String, Map < String, Object >> query(
            Map < String, Object > params) throws SourceAdapterException {

        String time = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss")
                .format(new Timestamp(System.currentTimeMillis()));

        Map < String, Map < String, Object >> result = new TreeMap < String, Map < String, Object >>();

        for (int i = 0; i < itemNum; i++) {
            Map < String, Object > record = new TreeMap < String, Object >();
            record.put("name", "Acme");
            record.put("industry", "Electronics");
            record.put("time", time);
            result.put(Integer.toString(i + 1), record);
        }

        return result;
    }

    public String create(Map < String, Object > params)
            throws SourceAdapterException {
        return Integer.toString(0);
    }

    public void update(Map < String, Object > attributes)
            throws SourceAdapterException {
        // TODO Auto-generated method stub

    }

    public void delete(Map < String, Object > attributes)
            throws SourceAdapterException {
        // TODO Auto-generated method stub

    }

    public void logoff() throws SourceAdapterException {
        // TODO Auto-generated method stub

    }

}
